package com.baizhi.bzmall.admin.controller;

import com.baizhi.bzmall.admin.service.RoleService;
import com.baizhi.bzmall.common.vo.ResultEntity;
import com.baizhi.bzmall.entity.BzAdminRole;
import com.baizhi.bzmall.entity.BzRole;
import com.baizhi.bzmall.entity.BzRoleResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService rs;
    @GetMapping
    @PreAuthorize("hasAuthority('system:admin:select')")
    public ResultEntity<List<BzRole>> selectAll(){
        List<BzRole> bzRoles = rs.selectAll();
        return ResultEntity.ok(bzRoles);
    }
    //根据用户编号查询用户的角色信息
    @GetMapping("/{adminId}")
    public ResultEntity<List<BzAdminRole>> selectRolesByUserId(@PathVariable("adminId") int adminId){
        List<BzAdminRole> bzAdminRoles = rs.selectRoleByAdmin(adminId);
        return ResultEntity.ok(bzAdminRoles);
    }
    @PutMapping("/{adminId}")
    public ResultEntity grantRoleToAdmin(@PathVariable("adminId") int adminId,@RequestBody int[] roleIds){
        System.out.println(adminId);
        //System.out.println(roleIds);
        for (int roleId : roleIds) {
            System.out.println(roleId);

        }
        return ResultEntity.ok();
    }
    @GetMapping("/permisssion/{roleId}")
    public ResultEntity<List<BzRoleResource>> selectPermessionByRoleId(@PathVariable("roleId") int roleId){
        List<BzRoleResource> bzRoleResources = rs.selectResourceByRole(roleId);
        return ResultEntity.ok(bzRoleResources);
    }
    @PutMapping("/permission/{roleId}")
    public ResultEntity grantPermissionToRole(@PathVariable("roleId") int roleId,@RequestBody int[] menuIds){
        rs.grantPermessionToRole(roleId,menuIds);
        return ResultEntity.ok();
    }
}
